
window.onload=initAll;

function initAll(){
	var myForm=document.getElementById('myform');
	myForm.onsubmit=function(){
		return this._email() && this._userPwd() && this._rePwd() && this._userName() && this._check();
	};
	checkForm();
}
function checkForm(){
	var myForm=document.getElementById('myform');
	myForm._email = function(){
		return this.email.value.isEmail();
	};
	
	myForm._userPwd = function(){
		var len=this.userPwd.value.length;
		if(len<6 || len>10){
			return false;
		}
	};
	
	myForm._rePwd = function(){
		var val=this.rePwd.value;
		if(val!=this.rePwd.value || val.length<6){
			return false;
		}
	};
	
	myForm._userName = function(){
		var len=this.userName.value.length;
		if(len>14 || len<7){
			return false;
		}
	};
	
	myForm._check = function(){
		var val=this.check.value;
		if(val!='  ' || val.length<4){     //获取验证码
			return false;
		}
	};
	
	var eles = myForm.elements;
	
	var notice,wrong;
	for (var i=0;i<eles.length;i++) {
		if (eles[i].tagName!= "INPUT") {
			continue;
		}
		notice = classGetter("notice",eles[i].parentNode)[0];
		if (notice) {
			(eles[i].notice = notice).style.display="none";
		eles[i].onfocus = showNotice;
		eles[i].onblur = hideNotice;
		wrong = classGetter("error",eles[i].parentNode)[0];
		}
		
		if (wrong) {
			eles[i].wrong=wrong;
		}
	}
	
	function showNotice() {
		
		this.notice.style.display ="inline-block";
		this.wrong.style.display ="none";
	}
	function hideNotice() {
		this.notice.style.display="none";
		if (myForm["_"+this.name]()===false) {
			this.wrong.style.display = "inline-block";
		} else {
			this.wrong.style.display = "none";
		}
		
	}
}


((String.prototype.isEmail = function () {return arguments.callee.re.test(this);}).re=new RegExp()).compile("^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$","i");

//hasClass 测试某DOM对象是否具有某className
function hasClass(cn,obj) {
	var re = new RegExp("\\b"+cn+"\\b");
	return re.test(obj.className);
}
//classGetter 获取root元素中具有某class的元素
function classGetter(cn,root) {
	root = root || document;
	if (root.getElementsByClassName && root.getElementsByClassName.constructor == Function) {
		return root.getElementsByClassName(cn);
	}
	var allTags = root.getElementsByTagName("*");
	var reArr = [];
	for (var i=0;i< allTags.length;i++) {
		if (hasClass(cn,allTags[i])) {
			reArr.push(allTags[i]);
		}
	}
	return reArr;
}
function nextElement(node) {
	var nextNode = node.nextSibling;
	if (!nextNode) {
		return null;
	}
	if (nextNode.nodeType!=1) {
		return nextNode.nextSibling?nextNode.nextSibling:null;
	}
}

